package com.sojson.util.security.springsecurity.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.sojson.base.impl.BaseUser;
import com.sojson.config.exception.TransErrorCode;
import com.sojson.enums.EEnvironmentUserType;
import com.sojson.enums.EUserType;
import com.sojson.enums.core.EStatusLogin;
import com.sojson.project.sys.user.service.UserAdminService;
import com.sojson.project.sys.user.service.UserMerchantService;
import com.sojson.project.sys.user.service.UserService;
import com.sojson.util.ExpUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.token.Token;
import com.sojson.util.token.TokenDto;
import com.sojson.util.token.TokenUtil;

/**
 * 获取用户逻辑
 *
 * @author liu
 * @date 2021-12-01
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserAdminService userAdminService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserMerchantService userMerchantService;

    /**
     * 获取用户信息(类似ShiroRealm)<BR/>
     * 调用AuthenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));的时候触发
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 获取参数
        TokenDto param = TokenUtil.getLoginParam();

        // 获取用户
        BaseUser user = null;
        try {
            String env = param.getLoginEnv();
            EEnvironmentUserType[] values = EEnvironmentUserType.values();
            for (EEnvironmentUserType envUserType : values) {
                if (envUserType.getCode().equals(env)) {
                    if (EUserType.ADMIN.getCode().equals(envUserType.getType())) {
                        user = userAdminService.loadUserByUsername(param, username);
                    } else if (EUserType.MERCHANT.getCode().equals(envUserType.getType())) {
                        user = userMerchantService.loadUserByUsername(param, username);
                    } else if (EUserType.USER.getCode().equals(envUserType.getType())) {
                        user = userService.loadUserByUsername(param, username);
                    }
                    break;
                }
            }
        } catch (Exception e) {
            // 记录用户登录日志
            TokenUtil.recordLogininfor(username, EStatusLogin.FAIL, e.getMessage());
            ExpUtil.throwEx(e);
        }

        // 判断登录的用户是否存在
        if (StringUtil.isBlankObject(user)) {
            // 记录用户登录日志
            TokenUtil.recordLogininfor(username, EStatusLogin.FAIL, TransErrorCode.NOT_USER.getMsg());
            ExpUtil.throwEx(TransErrorCode.NOT_USER);
        }

        // 设置统一的帐号
        user.setUsername(username);

        return createToken(user, param);
    }

    /**
     * 创建并初始化Token
     *
     * @param user
     * @param param
     * @return
     */
    public Token createToken(BaseUser user, Token param) {
        return new Token(user);
    }

}